class Solution
{
public:
    void duplicateZeros(vector<int>& arr)
    {
        int count = 0, i = 0;
        for (i = 0; i < arr.size(); i++)
        {
            if (arr[i])    count++;
            else    count += 2;

            if (count >= arr.size()) break;
        }

        int end = arr.size() - 1;
        if (count > arr.size())
        {
            arr[end] = 0;
            end--;
            i--;
        }

        while (i >= 0)
        {
            if (arr[i]) arr[end--] = arr[i--];
            else
            {
                arr[end--] = arr[i];
                arr[end--] = arr[i];
                i--;
            }
        }
    }
};